package apptoko.com.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;

public class CustomHorizontalScrollView extends HorizontalScrollView {

//	public CustomHorizontalScrollView(Context p_context, AttributeSet p_attrs) {
//		super(p_context, p_attrs);
//	}
//
//	@Override
//	public boolean onInterceptTouchEvent(MotionEvent ev) {
//		if (getParent() != null) {
//			switch (ev.getAction()) {
//			case MotionEvent.ACTION_MOVE:
//				getParent().requestDisallowInterceptTouchEvent(true);
//				break;
//			case MotionEvent.ACTION_UP:
//			case MotionEvent.ACTION_CANCEL:
//				getParent().requestDisallowInterceptTouchEvent(false);
//				break;
//			}
//		}
//
//		return super.onInterceptTouchEvent(ev);
//	}
//
//	@Override
//	public boolean onTouchEvent(MotionEvent p_event) {
//		if (p_event.getAction() == MotionEvent.ACTION_MOVE && getParent() != null) {
//			getParent().requestDisallowInterceptTouchEvent(true);
//		}
//		return super.onTouchEvent(p_event);
//	}
	private GestureDetector mGestureDetector;
    View.OnTouchListener mGestureListener;

    public CustomHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(context, new XScrollDetector());
        setFadingEdgeLength(0);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
    }

    // Return true if we're scrolling in the y direction  
    class XScrollDetector extends SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if(Math.abs(distanceY) > Math.abs(distanceX)) {
                return false;
            }
            return true;
        }
    }
}
